import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * service - 接收者类型维护
 * @date: 2018-7-26


 */
import request from 'utils/request';
import { CPAAS_MSG } from 'utils/config';
import { getCurrentOrganizationId, isTenantRoleLevel, parseParameters } from 'utils/utils';
var tenantId = getCurrentOrganizationId();

function receiverApi() {
  return isTenantRoleLevel() ? "".concat(tenantId, "/receiver-types") : 'receiver-types';
}
/**
 * 请求API前缀
 * @type {string}
 */


var prefix = "".concat(CPAAS_MSG, "/v1");
/**
 * 查询接收者类型列表数据
 * @async
 * @function fetchReceiverType
 * @param {object} params - 查询条件
 * @param {?string} params.typeCode - 类型编码
 * @param {?string} params.typeName - 描述
 * @param {!number} [params.page = 0] - 数据页码
 * @param {!number} [params.size = 10] - 分页大小
 * @returns {object} fetch Promise
 */

export function fetchReceiverType(_x) {
  return _fetchReceiverType.apply(this, arguments);
}
/**
 * 更新接收者类型信息
 * @async
 * @function updateReceiverType
 * @param {object} params - 请求参数
 * @param {!object} params.data - 更新对象
 * @param {!string} params.data.apiUrl - URL
 * @param {?number} params.data.enabledFlag - 启用标记
 * @param {!string} params.data.routeName - 服务
 * @param {!number} params.data.tenantId - 租户ID
 * @param {!string} params.data.typeCode - 类型编码
 * @param {!string} params.data.typeName - 描述
 * @returns {object} fetch Promise
 */

function _fetchReceiverType() {
  _fetchReceiverType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
    var param;
    return _regeneratorRuntime.wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            param = parseParameters(params);
            return _context.abrupt("return", request("".concat(prefix, "/").concat(receiverApi()), {
              method: 'GET',
              query: param
            }));

          case 2:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _fetchReceiverType.apply(this, arguments);
}

export function updateReceiverType(_x2) {
  return _updateReceiverType.apply(this, arguments);
}
/**
 * 添加接收者类型信息
 * @async
 * @function addReceiverType
 * @param {object} params - 请求参数
 * @param {!object} params.data - 保存对象
 * @param {!string} params.data.apiUrl - URL
 * @param {?number} params.data.enabledFlag - 启用标记
 * @param {!string} params.data.routeName - 服务
 * @param {!number} params.data.tenantId - 租户ID
 * @param {!string} params.data.typeCode - 类型编码
 * @param {!string} params.data.typeName - 描述
 * @returns {object} fetch Promise
 */

function _updateReceiverType() {
  _updateReceiverType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(params) {
    return _regeneratorRuntime.wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            return _context2.abrupt("return", request("".concat(prefix, "/").concat(receiverApi()), {
              method: 'PUT',
              body: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  }));
  return _updateReceiverType.apply(this, arguments);
}

export function addReceiverType(_x3) {
  return _addReceiverType.apply(this, arguments);
}
/**
 * 查询已经分配的行数据
 * @returns {Promise<object>}
 */

function _addReceiverType() {
  _addReceiverType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(params) {
    return _regeneratorRuntime.wrap(function _callee3$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", request("".concat(prefix, "/").concat(receiverApi()), {
              method: 'POST',
              body: _objectSpread({}, params)
            }));

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    }, _callee3);
  }));
  return _addReceiverType.apply(this, arguments);
}

export function queryAssignedList(_x4, _x5) {
  return _queryAssignedList.apply(this, arguments);
}
/**
 * 将 用户组 或者 组织 分配给接收者类型
 * @returns {Promise<object>}
 */

function _queryAssignedList() {
  _queryAssignedList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(receiverTypeId, params) {
    var newParams, isTenant, reqUrl;
    return _regeneratorRuntime.wrap(function _callee4$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            newParams = parseParameters(params);
            isTenant = isTenantRoleLevel();
            reqUrl = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(getCurrentOrganizationId(), "/receiver-types/lines/").concat(receiverTypeId) : "".concat(CPAAS_MSG, "/v1/receiver-types/lines/").concat(receiverTypeId);
            return _context4.abrupt("return", request(reqUrl, {
              method: 'GET',
              query: newParams
            }));

          case 4:
          case "end":
            return _context4.stop();
        }
      }
    }, _callee4);
  }));
  return _queryAssignedList.apply(this, arguments);
}

export function assignListToReceiverType(_x6, _x7) {
  return _assignListToReceiverType.apply(this, arguments);
}
/**
 * 移除已经分配给 接收者类型的行数据
 * @returns {Promise<void>}
 */

function _assignListToReceiverType() {
  _assignListToReceiverType = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(receiverTypeId, records) {
    var isTenant, reqUrl;
    return _regeneratorRuntime.wrap(function _callee5$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            isTenant = isTenantRoleLevel();
            reqUrl = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(getCurrentOrganizationId(), "/receiver-types/lines/").concat(receiverTypeId) : "".concat(CPAAS_MSG, "/v1/receiver-types/lines/").concat(receiverTypeId);
            return _context5.abrupt("return", request(reqUrl, {
              method: 'POST',
              body: records
            }));

          case 3:
          case "end":
            return _context5.stop();
        }
      }
    }, _callee5);
  }));
  return _assignListToReceiverType.apply(this, arguments);
}

export function removeReceiverTypeList(_x8, _x9) {
  return _removeReceiverTypeList.apply(this, arguments);
}
/**
 * 查询没有分配的 用户组
 * @returns {Promise<void>}
 */

function _removeReceiverTypeList() {
  _removeReceiverTypeList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(_
  /* receiverTypeId */
  , records) {
    var isTenant, reqUrl;
    return _regeneratorRuntime.wrap(function _callee6$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            isTenant = isTenantRoleLevel();
            reqUrl = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(getCurrentOrganizationId(), "/receiver-types/lines") : "".concat(CPAAS_MSG, "/v1/receiver-types/lines");
            return _context6.abrupt("return", request(reqUrl, {
              method: 'DELETE',
              body: records
            }));

          case 3:
          case "end":
            return _context6.stop();
        }
      }
    }, _callee6);
  }));
  return _removeReceiverTypeList.apply(this, arguments);
}

export function queryNoAssignUserGroupList(_x10, _x11) {
  return _queryNoAssignUserGroupList.apply(this, arguments);
}
/**
 * 查询没有分配的 组织
 * @returns {Promise<void>}
 */

function _queryNoAssignUserGroupList() {
  _queryNoAssignUserGroupList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(receiverTypeId, query) {
    var isTenant, reqUrl;
    return _regeneratorRuntime.wrap(function _callee7$(_context7) {
      while (1) {
        switch (_context7.prev = _context7.next) {
          case 0:
            isTenant = isTenantRoleLevel();
            reqUrl = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(getCurrentOrganizationId(), "/receiver-types/lines/user-groups/").concat(receiverTypeId) : "".concat(CPAAS_MSG, "/v1/receiver-types/lines/user-groups/").concat(receiverTypeId);
            return _context7.abrupt("return", request(reqUrl, {
              method: 'GET',
              query: parseParameters(query)
            }));

          case 3:
          case "end":
            return _context7.stop();
        }
      }
    }, _callee7);
  }));
  return _queryNoAssignUserGroupList.apply(this, arguments);
}

export function queryNoAssignUnitList(_x12, _x13) {
  return _queryNoAssignUnitList.apply(this, arguments);
} // 接收者类型用户组列表
// GET /v1/{organizationId}/receiver-types/lines/{receiverTypeId}
// 创建接收者类型用户组
// POST /v1/{organizationId}/receiver-types/lines
// 删除接收者类型用户组（批量）
// DELETE /v1/{organizationId}/receiver-types/lines

function _queryNoAssignUnitList() {
  _queryNoAssignUnitList = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(receiverTypeId, query) {
    var isTenant, reqUrl;
    return _regeneratorRuntime.wrap(function _callee8$(_context8) {
      while (1) {
        switch (_context8.prev = _context8.next) {
          case 0:
            isTenant = isTenantRoleLevel();
            reqUrl = isTenant ? "".concat(CPAAS_MSG, "/v1/").concat(getCurrentOrganizationId(), "/receiver-types/lines/units/").concat(receiverTypeId) : "".concat(CPAAS_MSG, "/v1/receiver-types/lines/units/").concat(receiverTypeId);
            return _context8.abrupt("return", request(reqUrl, {
              method: 'GET',
              query: parseParameters(query)
            }));

          case 3:
          case "end":
            return _context8.stop();
        }
      }
    }, _callee8);
  }));
  return _queryNoAssignUnitList.apply(this, arguments);
}